from django.shortcuts import render
from django.views import View

from contents.utils import index_category
from goods.models import GoodsCategory, GoodsChannel, ContentCategory


class IndexView(View):
    """展示首页界面"""

    def get(self, request):
        categories=index_category()
        contents = {}  # 用来包装所有广告数据
        # 查询所有广告类别数据
        category_ad = ContentCategory.objects.all()
        for i in category_ad:
            ad_data=i .content_set.filter(status=True).order_by('sequence')
            contents[i.key]=ad_data
        # 遍历广告类别查询集,来进行包装数据
            # 将每中类别下的所有广告查询出来,作为字典的value

        context = {
            'categories': categories,
            'contents': contents
        }
        return render(request, 'index.html', context)

